package com.example.zjx8969.myapplication.view

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.example.zjx8969.myapplication.DensityUtil
import com.example.zjx8969.myapplication.R

class TitleLayoutView @JvmOverloads constructor(context: Context, attr: AttributeSet? = null, def: Int = 0) : ConstraintLayout(context, attr, def), View.OnClickListener {
    private var clickListener: OnClickListener? = null
    var leftImageView: ImageView
    var leftCloseImageView: ImageView? = null
    var leftTextView: AppCompatTextView? = null
    var rightTextView: AppCompatTextView? = null
    var rightImageView: AppCompatImageView? = null
    var centerTextview: AppCompatTextView

    private val paddingValue: Int by lazy {
        DensityUtil.dip2px(context, 5f)
    }

    init {
        layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 48f))
        val type = context.obtainStyledAttributes(attr, R.styleable.TitleLayoutView)
        val leftBackVisibility = type.getBoolean(R.styleable.TitleLayoutView_leftBackVisible, true)
        val leftCloseVisibility = type.getBoolean(R.styleable.TitleLayoutView_leftCloseVisible, false)
        val leftTextContent = type.getString(R.styleable.TitleLayoutView_leftTextContent)
        val centerTextContent = type.getString(R.styleable.TitleLayoutView_title)
        val rightTextContent = type.getString(R.styleable.TitleLayoutView_rightTextViewContent)
        val rightIcon = type.getResourceId(R.styleable.TitleLayoutView_rightImageIcon, 0)
        setBackgroundColor(resources.getColor(R.color.color_2e))
        type.recycle()
        Log.e("tag", "leftBackVisibility=$leftBackVisibility,leftCloseVisibility=$leftCloseVisibility,leftTextContent=$leftTextContent,centerTextContent=$centerTextContent,rightTextContent=$rightTextContent,rightIcon=$rightIcon")
        leftImageView = AppCompatImageView(context).apply {
            id = R.id.titleLeftBack
            setPadding(paddingValue, 0, paddingValue, 0)
            setImageResource(R.drawable.btn_sel_title_back)
            setOnClickListener(this@TitleLayoutView)
            addViewWithParameters(this) {
                startToStart = LayoutParams.PARENT_ID
            }
            visibility = if (leftBackVisibility) VISIBLE else INVISIBLE
        }


        if (leftCloseVisibility) leftCloseImageView = AppCompatImageView(context).apply {
            id = R.id.titleLeftClose
            setImageResource(R.drawable.ic_cancel)
            setOnClickListener(this@TitleLayoutView)
            addViewWithParameters(this) {
                startToEnd = R.id.titleLeftBack
            }
/*            val constraintSet = ConstraintSet()
            constraintSet.connect(R.id.titleLeftClose, ConstraintSet.START, R.id.titleLeftBack, ConstraintSet.END)
            constraintSet.connect(R.id.titleLeftClose, ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP)
            constraintSet.connect(R.id.titleLeftClose, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM)
            constraintSet.applyTo(this@TitleLayoutView)*/
        }


        if (leftTextContent != null) leftTextView = AppCompatTextView(context).apply {
            id = R.id.titleLeftText
            maxLines = 1
            setPadding(paddingValue, 0, paddingValue, 0)
            setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
            text = leftTextContent
            addViewWithParameters(this) {
                startToStart = LayoutParams.PARENT_ID
            }
            setOnClickListener(this@TitleLayoutView)
        }

        if (rightTextContent != null) rightTextView = AppCompatTextView(context).apply {
            id = R.id.titleRightText
            maxLines = 1
            setPadding(paddingValue, 0, 3 * paddingValue, 0)
            setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
            text = rightTextContent
            setTextColor(Color.BLACK)
            addViewWithParameters(this) {
                endToEnd = LayoutParams.PARENT_ID
            }
            setOnClickListener(this@TitleLayoutView)
        }

        if (rightIcon != 0) rightImageView = AppCompatImageView(context).apply {
            id = R.id.titleRightImg
            setPadding(paddingValue, 0, 3 * paddingValue, 0)
            scaleType = ImageView.ScaleType.CENTER_INSIDE
            setImageResource(rightIcon)
            addViewWithParameters(this) {
                endToEnd = LayoutParams.PARENT_ID
            }
            setOnClickListener(this@TitleLayoutView)
        }

        centerTextview = AppCompatTextView(context).apply {
            id = R.id.titleCenterText
            gravity = Gravity.CENTER
            maxLines = 1
            ellipsize = TextUtils.TruncateAt.END
            setTextSize(TypedValue.COMPLEX_UNIT_SP, 18f)
            setTextColor((0xFF2E2E2E).toInt())
            addViewWithParameters(this, LayoutParams.MATCH_PARENT) {
                val margins = DensityUtil.dip2px(context, 80f)
                setMargins(margins, 0, margins, 0)
            }
            text = centerTextContent
            setOnClickListener(this@TitleLayoutView)
        }

        View(context).apply {
            setBackgroundColor(Color.parseColor("#d9d9d9"))
            layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dp2px(1f).toInt()).apply {
                startToStart = LayoutParams.PARENT_ID
                bottomToBottom = LayoutParams.PARENT_ID
                endToEnd = LayoutParams.PARENT_ID
            }
            addView(this)
        }
    }

    fun addViewWithParameters(view: View, width: Int = LayoutParams.WRAP_CONTENT,
                              height: Int = LayoutParams.WRAP_CONTENT,
                              params: LayoutParams.() -> Unit) {
        view.layoutParams = LayoutParams(width, height).also {
            it.topToTop = LayoutParams.PARENT_ID
            it.bottomToBottom = LayoutParams.PARENT_ID
            params(it)
        }
        addView(view)
    }

    override fun setOnClickListener(l: OnClickListener?) {
        super.setOnClickListener(l)
        clickListener = l
    }

    override fun onClick(v: View?) {
        clickListener?.onClick(v)
    }
}